package com.chencs.common.menu.controller;

import com.alibaba.fastjson.JSON;
import com.chencs.common.menu.service.MenuService;
import com.chencs.vo.common.menu.MenuModel;
import framework.model.ResponseOptUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * Created by fangzehua on 2017/6/15.
 */
@RequestMapping("/menu")
@RestController
public class MenuController {

    private static final Logger logger = LoggerFactory.getLogger(MenuController.class);

    @Autowired
    MenuService menuService;

    @RequestMapping(value = "/menuList", method = RequestMethod.GET)
    public ModelAndView main() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("admin/common/menu/menu_list");
        return modelAndView;
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list() {
        logger.info("/role/list param: {}", "");
        String retStr = "";
        try {
            retStr = menuService.getList();
        } catch (Exception e) {
            retStr = ResponseOptUtil.commonMsgError( ResponseOptUtil.RET_FAIL_MSG);
            e.printStackTrace();
        }
        return retStr;
    }

    @RequestMapping(value = "/doAddMenuPage", method = RequestMethod.GET)
    public ModelAndView doRolePage(Model model) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("admin/common/menu/menu_add");
        List<MenuModel> models = null;
         try{
             models = JSON.parseArray(menuService.getList(),MenuModel.class);
         }catch (Exception e){
            e.printStackTrace();
         }
        model.addAttribute("models",models);
        return modelAndView;
    }

    @RequestMapping(value = "/addMenu", method = RequestMethod.POST)
    public String addMenu(@RequestBody MenuModel model) {
        logger.info("/menu/a param: {}", JSON.toJSON(model) );
        String retStr = "";
        try {
            retStr = menuService.addMenu(model);
        } catch (Exception e) {
            retStr = ResponseOptUtil.commonMsgError( ResponseOptUtil.RET_FAIL_MSG);
            e.printStackTrace();
        }
        return retStr;
    }


    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public String delete(@RequestBody MenuModel model) {
        logger.info("/menu/delete param: {}", JSON.toJSON(model));
        String retStr = "";
        try {
            retStr = menuService.delete(model);
        } catch (Exception e) {
            retStr = ResponseOptUtil.commonMsgError( ResponseOptUtil.RET_FAIL_MSG);
            e.printStackTrace();
        }
        return retStr;
    }


}
